/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.praticas.projetoacademia.dao;

import br.praticas.projetoacademia.models.Endereco;
import br.praticas.projetoacademia.util.HibernateUtil;
import java.util.List;
import org.hibernate.classic.Session;

/**
 *
 * @author lucia
 */
public class EnderecoDao {

    private Session sessao;

    public void add(Endereco endereco) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        sessao.save(endereco);
        sessao.getTransaction().commit();
        sessao.close();
    }

    public void update(Endereco endereco) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        sessao.update(endereco);
        sessao.getTransaction().commit();
        sessao.close();
    }

    public void remove(Endereco endereco) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        sessao.delete(endereco);
        sessao.getTransaction().commit();
        sessao.close();
    }

   

     public List<Endereco> listarEndereco() {
        sessao = HibernateUtil.getSessionFactory().openSession();
        List<Endereco> lista = sessao.createCriteria(Endereco.class).list();
        sessao.close();
        return lista;
    }

    public Endereco Retornainstrutor(Long id) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        Endereco endereco = (Endereco) sessao.get(Endereco.class, id);
        sessao.getTransaction().commit();
        sessao.close();
        return endereco;
    }

    public Session getSessao() {
        return this.sessao;
    }

}
